package com.wpam.scrabble.mobile.activities;

import java.util.ArrayList;
import java.util.Collections;

import com.wpam.scrabble.mobile.R;
import com.wpam.scrabble.mobile.ScoreComparable;

import android.os.Bundle;
import android.util.Pair;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TextView;
import android.app.Activity;

public class GameResultsActivity extends Activity {

	private LinearLayout layout_;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game_results);
		
		layout_ = (LinearLayout) findViewById(R.id.resultsLayout);
		ArrayList<String> players = getIntent().getStringArrayListExtra("Players");
		ArrayList<Integer> points = getIntent().getIntegerArrayListExtra("Points");
		ArrayList<Pair<String, Integer>> results = new ArrayList<Pair<String,Integer>>();
		
		String playerName = null;
		for (int i = 0; i < players.size(); ++i) {
			playerName = players.get(i);
			results.add(new Pair<String, Integer>(playerName, points.get(i)));
		}
		
		Collections.sort(results, new ScoreComparable());
		
		for (int i = 0; i < results.size(); ++i) {
			LinearLayout lay = new LinearLayout(this);
			lay.setOrientation(LinearLayout.HORIZONTAL);
			TextView name = new TextView(this);
			name.setText(String.valueOf(i+1) + ". " + results.get(i).first);
			name.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
			name.setGravity(Gravity.LEFT);
			name.setPadding(5, 0, 0, 0);
			name.setTextAppearance(this, android.R.style.TextAppearance_Medium);
			TextView point = new TextView(this);
			point.setText(String.valueOf(results.get(i).second));
			point.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 10));
			point.setGravity(Gravity.RIGHT);
			point.setPadding(0, 0, 5, 0);
			point.setTextAppearance(this, android.R.style.TextAppearance_Medium);
			lay.addView(name);
			lay.addView(point);
			layout_.addView(lay);
		}
	}

}
